<template>
  <div>
    <el-button
      type="primary"
      size="default"
      icon="el-icon-plus"
      style="margin-bottom: 10px"
      @click="dialogFormVisible = true"
      >添加</el-button
    >
    <el-table :data="categoryList" border stripe>
      <el-table-column label="序号" width="80px" align="center">
        <template v-slot="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="种类"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="editData(row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteItem(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="cForm.id ? '修改种类' : '添加种类'"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form :model="cForm" ref="myForm" style="width: 80%" :rules="rules">
        <el-form-item label="种类名称" label-width="100px" prop="name">
          <el-input v-model="cForm.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="submitData">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Category",
  data() {
    //自定义校验规则
    var validateName = (rule, value, callback) => {
      //自定义校验规则
      if (value.length < 2 || value.length > 10) {
        callback(new Error("种类名称2-10位"));
      } else {
        callback();
      }
    };
    return {
      categoryList: [],
      dialogFormVisible: false,
      cForm: {
        name: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入种类名称", trigger: "blur" },
          {
            validator: validateName,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.getCategoryList();
  },
  methods: {
    async getCategoryList() {
      let ret = await reqCategoryList();
      if (ret.code == 200) this.categoryList = ret.data;
    },
    // 点击取消按钮
    cancelDialog() {
      this.cForm.name = "";
      this.cForm.id = undefined;
      this.dialogFormVisible = false;
    },
    // 点击叉号关闭
    handleClose(done) {
      this.$confirm("确认关闭？", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((_) => {
          this.cancelDialog();
          done();
        })
        .catch((_) => {});
    },
    submitData() {
      this.$refs.myForm.validate(async (valid) => {
        if (valid) {
          let ret = await reqAddOrUpdateCategory(this.cForm);
          console.log(ret);
          let tip = this.cForm.id ? "种类修改成功" : "种类添加成功";
          if (ret.code == 200) {
            this.$message.success(tip);
            this.cancelDialog();
            this.getCategoryList();
          }
        } else {
          this.$message.error("请输入正确的数据");
          return false;
        }
      });
    },
    editData(item) {
      this.dialogFormVisible = true;
      this.cForm = { ...item };
    },
    deleteItem(row) {
      this.$confirm(`你确定删除${row.name}?`, "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //当用户点击确定按钮的时候会出发
          //向服务器发请求
          let result = await reqDeleteCategory({ id: row.id });
          //如果删除成功
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //再次获取种类列表数据
            this.getCategoryList();
          }
        })
        .catch(() => {
          //当用户点击取消按钮的时候会触发
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style></style>
